// 清理未使用的效果 (隐藏未使用的效果)
toolsRemoveUnusedFX();
function toolsRemoveUnusedFX(){
    var aePILEScrpt = new Object();
    aePILEScrpt.projectFile = app.project.file;
    var isProjectAvailable = aePILEScrpt.projectFile;
    if (isProjectAvailable != null) {
        //toolsConsolidateResults.text = "";
        youSure = confirm("不保留 表达式 引用的 效果.\n继续？", true);
        if (youSure == true) {
            app.beginUndoGroup("Remove unused effects");
            try {
                gone = removeUnusedFX();
                startedWith = gone[0];
                remain = gone[1];
                removed = gone[0] - gone[1];
            } catch (err) {
                alert("ERROR:\nSomething went wrong, please email torno@sydefxink.com with the info below:\n--------------------------------------\nOS: " + $.os + " (" + $.locale + ")\nAE: " + app.version + " (" + app.language + ")\n\nError at line #" + err.line + "\n\n" + err.toString());
            }
            app.endUndoGroup();
        }
    } else {
        if (app.project.numItems > 0) {
            alert("There's things in the panel, but we need to save the project before we mess with it.\n\nYou can click \"Cancel\" in the next dialog if you change your mind.");
            var userSaved = app.project.saveWithDialog();
            if (userSaved != false) {
                toolsRemoveUnusedFX.onClick();
            }
        } else {
            alert("Hmmm, you must be using the new invisible project mode.\n\nPlease open a project file and try again.");
        }
    }
}

function removeUnusedFX() {
    fxCt = 0;
    fxRemoveCt = 0;
    compAry = [];
    layerAry = [];
    proj = app.project;
    itemCount = proj.numItems;

    function removeProcess(effectCount, effectGroup, allOff) {
        if (allOff == "off") {
            for (var e = effectCount; e > 0; e--) {
                curEffect = effectGroup.property(e);
                if (curEffect.enabled == false) {
                    curEffect.remove();
                }
            }
        } else {
            for (var e = effectCount; e > 0; e--) {
                effectGroup.property(e).remove();
            }
        }
    }
    for (var i = 1; i <= itemCount; i += 1) {
        curItem = proj.item(i);
        if (curItem instanceof CompItem) {
            compAry.push(curItem);
        }
    }
    compCount = compAry.length;
    fxCt = getFXTotal();
    for (var c = 0; c < compCount; c += 1) {
        curComp = compAry[c];
        layerAry = curComp.layers;
        layerCount = layerAry.length;
        for (var l = 1; l <= layerCount; l += 1) {
            curLayer = layerAry[l];
            if (!(curLayer instanceof CameraLayer) && !(curLayer instanceof LightLayer)) {
                writeLn(curLayer.containingComp.name + " / " + curLayer.index.toString() + " / " + curLayer.name);
                effectGroup = curLayer.property("ADBE Effect Parade");
                effectCount = effectGroup.numProperties;
                if (effectCount > 0) {
                    if (curLayer.effectsActive == false) {
                        removeProcess(effectCount, effectGroup, "all");
                    } else {
                        if (curLayer.enabled == false && curLayer.isTrackMatte == false) {
                            removeProcess(effectCount, effectGroup, "all");
                        } else if (curLayer.enabled == false && curLayer.isTrackMatte == true) {
                            removeProcess(effectCount, effectGroup, "off");
                        } else {
                            removeProcess(effectCount, effectGroup, "off");
                        }
                    }
                }
            }
        }
    }
    fxRemoveCt = getFXTotal();
    return [fxCt, fxRemoveCt];
}

function getFXTotal() {
    var proj = app.project;
    var projItems = proj.items;
    var projItemsLen = projItems.length;
    var count = 0;
    for (var i = 1; i <= projItemsLen; i += 1) {
        curItem = projItems[i];
        if (curItem instanceof CompItem) {
            layers = curItem.layers;
            layersLen = layers.length;
            for (var l = 1; l <= layersLen; l += 1) {
                curLayer = layers[l];
                curLayer.property("ADBE Effect Parade") != null ? count += curLayer.property("ADBE Effect Parade").numProperties : "";
            }
        }
    }
    return count;
}